//
//  FSCategoryDetailVC.m
//  FieldStory
//
//  Created by itte on 2017/6/22.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSCategoryDetailVC.h"
#import "LineLayout.h"
#import "CategoryDetailCell.h"

@interface FSCategoryDetailVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSInteger index;
}
@property (weak, nonatomic) IBOutlet UICollectionView *cltDetailView;
@property (weak, nonatomic) IBOutlet UILabel *lbNum;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lcsWidthOfNum;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation FSCategoryDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = self.params[@"data"];
    index = [self.params[@"index"] integerValue];
    LineLayout *layout = [[LineLayout alloc] init];
    [self.cltDetailView setCollectionViewLayout:layout];
    self.cltDetailView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.cltDetailView.dataSource = self;
    self.cltDetailView.delegate = self;
    
    [self.cltDetailView registerNib:[UINib nibWithNibName:@"CategoryDetailCell" bundle:nil] forCellWithReuseIdentifier:@"CategoryDetailCellID"];
    [self.cltDetailView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.cltDetailView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


#pragma mark - 按钮点击事件
- (IBAction)btnChangeNum:(UIButton *)sender
{
    int value = [self.lbNum.text intValue];
    if (sender.tag == 600) {
        self.lbNum.text = [NSString stringWithFormat:@"%d", ++value];
        return ;
    }
    if (value <= 1) {
        self.lbNum.text = @"1";
    }
    else{
        self.lbNum.text = [NSString stringWithFormat:@"%d", --value];
    }
}

- (IBAction)btnChoiseClick:(id)sender
{
    NSInteger pageIndex = self.cltDetailView.contentOffset.x/SCREEN_WIDTH+0.5;
    NSLog(@"--%ld",pageIndex);
    [self pushViewController:@"FSFarmerVC" withParams:@{@"data":self.dataArray[pageIndex],@"number":self.lbNum.text.trimString}];
}

#pragma mark - Navigation
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryDetailCellID" forIndexPath:indexPath];
    [cell.btnClose addTarget:self action:@selector(backup) forControlEvents:UIControlEventTouchUpInside];
    cell.subCategory = self.dataArray[indexPath.row];
    [cell.tableView reloadData];
    return cell;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.cltDetailView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    return NO;
}

@end
